package com.shopping.control;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shopping.dao.impl.BizDaoImpl;
import com.shopping.entity.TbMember;
import com.shopping.service.impl.BizServiceImpl;

/**
 * Servlet implementation class fg_threeServlet
 */
public class fg_threeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public fg_threeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // TODO Auto-generated method stub
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");

        BizDaoImpl bdi = new BizDaoImpl();
        BizServiceImpl bsi = new BizServiceImpl();
        bsi.setBizdao(bdi);// 注入

        String password = request.getParameter("password");
        String passwordOne = request.getParameter("passwordOne");

        HttpSession session = request.getSession();

        PrintWriter out = response.getWriter();
        if (password.equals("") || passwordOne.equals("")) {
            out.print("<script>");
            out.print("alert('表单信息不能为空!');");
            out.print("window.location.href='fg-three.jsp';");
            out.print("</script>");
            out.flush();
            out.close();
            return;
        }

        if (password.equals(passwordOne)) {
            TbMember tb = (TbMember) session.getAttribute("men");
            if (password.equals(tb.getPassword())) {
                request.setAttribute("flag_four", 2);
                request.getRequestDispatcher("fg-four.jsp").forward(request, response);
            } else {
                tb.setPassword(password);
                int i = bsi.updatetbmember(tb);
                if (i > 0) {
                    request.setAttribute("flag_four", 1);
                    request.getRequestDispatcher("fg-four.jsp").forward(request, response);
                }
            }

        } else {
            out.print("<script>");
            out.print("alert('两次密码输入不一致!');");
            out.print("window.location.href='fg-three.jsp';");
            out.print("</script>");
            out.flush();
            out.close();
            return;
        }
    }

}
